/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.can.bc;

import edu.upn.sigemfi.can.be.TransaccionWeb;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Programador
 */
public class TransaccionWebDelegate {

    private TransaccionWebFacadeLocal local;

    public TransaccionWebDelegate(){
        local = lookupTransaccionWebFacade();
    }

    public void crear(TransaccionWeb transaccionWeb) throws Exception{
        try {
            local.create(transaccionWeb);
        } catch (Exception e) {
            local=lookupTransaccionWebFacade();
            throw new Exception("Crear Transaccion Web" + e.getMessage(), e);
        }
    }

    public void editar(TransaccionWeb transaccionWeb) throws Exception{
        try {
            local.edit(transaccionWeb);
        } catch (Exception e) {
            local=lookupTransaccionWebFacade();
            throw new Exception("Editar Transaccion Web" + e.getMessage(), e);
        }
    }

    public void eliminar(TransaccionWeb transaccionWeb) throws Exception{
        try {
            local.remove(transaccionWeb);
        } catch (Exception e) {
            local=lookupTransaccionWebFacade();
            throw new Exception("Eliminar Transaccion Web" + e.getMessage(), e);
        }
    }

    public TransaccionWeb buscar(long codigo) throws Exception{
        try {
            return local.find(codigo);
        } catch (Exception e) {
            local=lookupTransaccionWebFacade();
            throw new Exception("Buscar Transaccion Web" + e.getMessage(), e);
        }
    }

    public List<TransaccionWeb> listar() throws Exception {
        try {
            return local.findAll();
        } catch (Exception e) {
            local = lookupTransaccionWebFacade();
            throw new Exception("Listar Transaccion Web. " + e.getMessage(), e);
        }
    }

    private TransaccionWebFacadeLocal lookupTransaccionWebFacade() {
        try {
            Context c = new InitialContext();
            return (TransaccionWebFacadeLocal) c.lookup("java:comp/env/TransaccionWebFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }



}
